package Lab12Package;

//Java 数组

public class Lab12_05 {
	public static void main(String[] args) {
		int[] arrayRefVar1 = new int[20];; // 首选的方法
		int arrayRefVar2[] = new int[20];; // 效果相同，但不是首选方法

		// 也可以定义之后就初始化
		int[] arrayRefVar3 = {0, 1, 2};
		System.out.println(arrayRefVar3);

		// 示例1
		System.out.println("\n示例1");
		int size = 10;// 数组大小
		double[] myList = new double[size];// 定义数组
		myList[0] = 5.6;
		myList[1] = 4.5;
		myList[2] = 3.3;
		myList[3] = 13.2;
		myList[4] = 4.0;
		myList[5] = 34.33;
		myList[6] = 34.0;
		myList[7] = 45.45;
		myList[8] = 99.993;
		myList[9] = 11123;
		// 计算所有元素的总和
		double total = 0;
		for (int i = 0; i < size; i++) {
			total += myList[i];
		}
		System.out.println("总和为： " + total);

		// 示例2
		System.out.println("\n示例2");
		double[] myList1 = {1.9, 2.9, 3.4, 3.5};
		// 打印所有数组元素
		for (int i = 0; i < myList1.length; i++) {
			System.out.println(myList1[i] + " ");
		}
		// 计算所有元素的总和
		double total1 = 0;
		for (int i = 0; i < myList1.length; i++) {
			total1 += myList1[i];
		}
		System.out.println("Total is " + total1);
		// 查找最大元素
		double max = myList1[0];
		for (int i = 1; i < myList1.length; i++) {
			if (myList1[i] > max)
				max = myList1[i];
		}
		System.out.println("Max is " + max);
	}

}
